package com.woniu.service.impl;


import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniu.commons.exception.YogaException;
import com.woniu.commons.model.StateData;
import com.woniu.dao.YCoachTypeMapper;
import com.woniu.model.dto.CoachTypeDto;
import com.woniu.model.entity.YCoachType;
import com.woniu.model.entity.YUser;
import com.woniu.model.param.AddCoachTypeParam;
import com.woniu.model.param.PageCoachTypeParam;
import com.woniu.model.param.PageParam;
import com.woniu.service.YCoachTypeService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 胡歌
 * @since 2024年05月22日
 */
@Service
public class YCoachTypeServiceImpl extends ServiceImpl<YCoachTypeMapper, YCoachType> implements YCoachTypeService {
    @Resource
    private  YCoachTypeMapper coachTypeMapper;

    @Override
    public void addCoachType(AddCoachTypeParam param) {
        QueryWrapper<YCoachType> wrapper = new QueryWrapper<>();
        wrapper.eq("coach_type_name", param.getCoachTypeName());
        wrapper.ne("coach_type_state",StateData.CLASS_TYPE_STATE_DELETED);
        YCoachType existingType = coachTypeMapper.selectOne(wrapper);
        if (existingType != null && existingType.getCoachTypeState()!=StateData.CLASS_TYPE_STATE_DELETED) {
            throw new YogaException(50007, "教练类型已存在");
        }
        YCoachType coachType = new YCoachType();
        coachType.setCoachTypeName(param.getCoachTypeName());
        coachType.setCoachTypeState(StateData.CLASS_TYPE_STATE_DISABLED);

        coachTypeMapper.insert(coachType);
    }

    @Override
    public void disable(Integer coachTypeId) {
        YCoachType coachType = coachTypeMapper.selectById(coachTypeId);
        if(Objects.equals(coachType.getCoachTypeState(), StateData.COACHTYPE_STATE_DISABLED)){
            throw new YogaException(20001,"类别状态异常");
        }
       coachType.setCoachTypeState(0);
        coachTypeMapper.updateById(coachType);

    }

    @Override
    public void enable(Integer coachTypeId) {
        YCoachType coachType = coachTypeMapper.selectById(coachTypeId);
        if(Objects.equals(coachType.getCoachTypeState(), StateData.COACHTYPE_STATE_NORMAL)){
            throw new YogaException(20001,"类别状态异常");
        }
        coachType.setCoachTypeState(1);
        coachTypeMapper.updateById(coachType);
    }

    /**
     * 分页查询所有类型
     * @param param
     * @return
     * @throws Exception
     */
    @Override
    public Page<YCoachType> pageAllCoachType(PageCoachTypeParam param)throws YogaException {
        if (param.getPageNum() == null) {
            param.setPageNum(1);
        }
        if (param.getPageSize() == null) {
            param.setPageSize(5);
        }

        //拼接条件
        QueryWrapper<YCoachType> wrapper = new QueryWrapper<YCoachType>();
        wrapper.like("coach_type_name",param.getCoachTypeName());
        wrapper.ne("coach_type_state",StateData.CLASS_TYPE_STATE_DELETED);

        return baseMapper.selectPage(new Page<YCoachType>(param.getPageNum(), param.getPageSize()), wrapper);
    }

    /**
     * 根据ID删除类别
     * @param classTypeId
     * @return
     */
    @Override
    public void classTypeId(Integer classTypeId) {
        YCoachType yCoachType = baseMapper.selectById(classTypeId);
        if (yCoachType==null){
            throw new YogaException(500,"删除失败，还没有该类别");
        }
        if (yCoachType.getCoachTypeState()==StateData.CLASS_TYPE_STATE_DELETED){
            throw new YogaException(500,"删除失败，已是删除状态");
        }
        if (yCoachType.getCoachTypeState()==StateData.CLASS_TYPE_STATE_START){
            throw new YogaException(500,"正在使用中，删除失败");
        }

        yCoachType.setCoachTypeState(StateData.CLASS_TYPE_STATE_DELETED);
        baseMapper.updateById(yCoachType);
    }

    /**
     * 查询集合回去
     * @return
     */
    @Override
    public List<CoachTypeDto> seatInfoList() {
        QueryWrapper<YCoachType> wrapper = new QueryWrapper<>();
        wrapper.ne("coach_type_state",2);
        List<YCoachType> lists = baseMapper.selectList(wrapper);

        List<CoachTypeDto> list=new ArrayList<>();
        for (YCoachType info : lists) {
            list.add(BeanUtil.toBean(info,CoachTypeDto.class));
        }

        return list;
    }


}
